package com.itany.ums.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

@WebServlet(name = "UserServlet", urlPatterns = "*.user")
public class UserServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();

        if ("/regist.user".equals(path)) {
            regist(request, response);
        }
    }

    protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Connection con = null;

        PreparedStatement ps = null;

        ResultSet rs = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");

            con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ums?useUnicode=true&characterEncoding=utf8", "root", "123");

            con.setAutoCommit(false);

            String sql = new StringBuffer()
                    .append("select * ")
                    .append("from t_user ")
                    .append("where username=? ")
                    .toString();
            ps=con.prepareStatement(sql);
            ps.setString(1,username);
            rs = ps.executeQuery();

            if (rs.next()) {
                request.setAttribute("message", "用户名已存在");
                request.getRequestDispatcher("/regist.jsp").forward(request, response);
                con.commit();
                return;
            }

            sql = new StringBuffer()
                    .append("insert into t_user ")
                    .append("    (username,password) ")
                    .append("values ")
                    .append("    (?,?) ")
                    .toString();
            ps = con.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            ps.executeUpdate();

            response.sendRedirect(request.getContextPath()+"/success.jsp");

            con.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                ;
            }
        }

    }

}
